var status_code = function (code) {
  //权限
  switch (code) {
    case 10002:
      return "你没有该操作权限";
      break;
    case 10000:
      return "你的账户已过期，或在异地登录，请重新登陆";
      break;
    default:
      return true
  }
};
var nueTime = function (time1, time2) {
  // 获取时间
  var start = time1;
  var End = time2;
  var statea = start.getMonth() + 1;
  var starttime = start.getFullYear() + '-' + (this.Appendzero(statea)) + '-' + this.Appendzero(start.getDate()) + ' ' + '00' + ':' + '00' + ':' + '00';
  var endstate = End.getMonth() + 1;
  var Endtime = End.getFullYear() + '-' + (this.Appendzero(endstate)) + '-' + this.Appendzero(End.getDate()) + ' ' + 23 + ':' + 59 + ':' + '59';
  return [starttime, Endtime];
}
var Appendzero = function (obj) {
  ////  日期 不够两位数 就在前面加一个0
  if (obj < 10) return "0" + "" + obj;
  else return obj;
};

var subpicture = function ($e, path_name) {//图片上传
  return new Promise(function (resolve, reject) {
    let doc = $e.target;
    let file = doc.files[0];
    let reader = new FileReader();
    let formdata = new FormData();       //js    创建form表单
    reader.readAsDataURL(file);
    reader.onload = (e) => {
      let request = new XMLHttpRequest();
      request.open("POST", "http://image.myjytech.com/uploadimage");
      formdata.append('image', file);
      formdata.append('pathname', path_name);
      request.send(formdata);
      request.onreadystatechange = () => {
        if (request.status === 200 && request.readyState === 4) {
          let res = request.response;
          if (res != '') {
            resolve(JSON.parse(res));
          }
        }
      }
    }
  });
};
var EXCel = function (api, data) {
  //PHP下载提现表格
  window.location.href = "http://chuanyin.myjytech.com/Server/public/api/admin/" + api + "?starttime=" + data.starttime + '&endtime=' + data.endtime + '&type=' + data.type + '&phone=' + data.type;
};

var RedEXCel = function (api, data) {
  //PHP下载提现表格
  window.location.href = "http://lu.ngrok.jinyitech.cn/api/businesses/" + api + "?id=" + data;
};
var dataSuccess = function (val, this_) {
  //成功的返回
  this_.$notify({
    title: '成功',
    message: val.Massage,
    type: 'success'
  });
};
var formattime = function (time) {
  //设置时间
  let endTime = new Date(time);
  var hour = endTime.getHours() < 10 ? "0" + endTime.getHours() : endTime.getHours();
  var minute = endTime.getMinutes() < 10 ? "0" + endTime.getMinutes() : endTime.getMinutes();
  var second = endTime.getSeconds() < 10 ? "0" + endTime.getSeconds() : endTime.getSeconds();
  return `${endTime.getFullYear()}年${endTime.getMonth() + 1}月${endTime.getDate()}日:${hour}时-${minute}分-${second}秒`
};


var storage = '';
var SetStorage = function (k, v) {
  storage = v;
  // console.log(storage);
  //添加 localStorage
  window.localStorage.removeItem(k);
  window.localStorage.setItem(k, v);
};
var GetStorage = function (k) {
  //读取 localStorage
  // return window.localStorage.getItem(k);
  if(storage == ''){
    return window.localStorage.getItem(k);
  }else{
    return storage
  }
};


var Ed = function (val, this_) {
  //分返错误
  this_.$notify.error({
    title: '错误',
    message: '用户信息已更新请重新登陆'
  });
}.bind(this);


var userphone_test = function (val, this_) {
  //电话号码正则
  var reg = /^[1-9]\d*$|^0$/;
  if (reg.test(val) == true) {
    if (val.length == 11) {
      return true;
    } else {
      this_.$notify.error({
        title: '错误',
        message: '您输入的电话号码长度不正确'
      });
    }
  } else {
    this_.$notify.error({
      title: '错误',
      message: '您输入的格式不正确！'
    });
  }
};


var user_type = function (type) {
  //角色过滤
  switch (type) {
    case 1:
      return "管理员";
      break;
    case 2:
      return "员工";
      break;
    case 3:
      return "一级代理商";
      break;
    case 4:
      return "二级代理商";
      break;
    case 5:
      return "用户";
      break;
  };
};
var conHeight = '';
let ContentBodyHeight = function (val) {
  conHeight = val;
};
let GetContentBodyHeight = function () {
  return conHeight ;
};


let addCookie = function (objName,objValue,objHours){
  //添加新的cookie
    var str = objName + "=" + objValue;

  //为0时不设定过期时间，浏览器关闭时cookie自动消失
  // if(objHours > 0){
  //   var date = new Date();
  //   var ms = objHours*3600*1000;
  //   date.setTime(date.getTime() + ms);
  //   str += "; expires=" + date.toString();
  // }
  document.cookie = str;
  // console.log(objValue);
};

 let getCookie = function (objName) {
   //获取指定名称的cookie的值
   var arrStr = document.cookie.split("=");
   console.log(arrStr);
   // return unescape(arrStr[1])
   // for (var i = 0; i < arrStr.length; i++) {
   //   var temp = arrStr[i].split("=");
   //   if (temp[0] == objName) return unescape(temp[1]);
   // }
 };
export default{
  status_code,  //权限
  dataSuccess,  //成功的返回
  formattime,   //设置时间
  storage,   //储存localStorage
  SetStorage,   //添加 localStorage
  GetStorage,   //读取 localStorage
  Ed,           //分返错误
  user_type,    //角色过滤
  userphone_test, //电话号码正则
  subpicture,     //图片提交
  Appendzero,     // 日期 不够两位数 就在前面加一个0
  nueTime,  //返回时间
  EXCel,//下载表格
  ContentBodyHeight,
  GetContentBodyHeight,
  RedEXCel,
  addCookie,//添加新的cookie
  getCookie//获取指定名称的cookie的值
};
